﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Vms.DB;
using Vms.Business;
using System.IO;
using System.Text;
public partial class XoSo_KetQua : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Util m_util = new Util();
        DB m_db = new DB();
        WriteFile wf = new WriteFile();
        Util.TCharging m_tCharging = new Util.TCharging();
        try
        {
            int e_variable = 0;
            string d_variable = "";
            int Service_id = 0;
            string tinh_id = "";
            string str = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
                    "<!DOCTYPE pages SYSTEM 'cellflash.dtd'>" +
                    "<pages descr='Samis'>";
            if (Request.Params["day"] != null)
            {
                m_util.write_log(Request.Params["day"], 2);
            }
            if (Request.Params["e"] != null)
            {
                e_variable = Int32.Parse(Request.Params["e"]);
            }
            if (Request.Params["Tinhid"] != null)
            {
                tinh_id = Request.Params["Tinhid"];
            }
            if (Request.Params["desc"] != null)
            {
                d_variable = Request.Params["desc"];
            }
            if (Request.Params["service_id"] != null)
            {
                Service_id = Int32.Parse(Request.Params["service_id"]);
            }
            Util.TSession m_tSession = new Util.TSession();
            m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
            m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
            m_tSession.SessionID = "";
            m_tSession.IP = "";
            if (tinh_id != "0")
            {
                Service_id = 21;
                m_tCharging.ErrorCode = e_variable;
                m_tCharging.ErrorDescription = d_variable;
                m_tCharging.Service_ID = Service_id;
                m_tCharging.SessionID = Session.SessionID;
                m_tCharging.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tCharging.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tCharging.StartTime = DateTime.Now;
                if (e_variable == 0)
                {
                    m_tCharging.Cost = m_db.GetCostByService(m_tCharging.Service_ID);
                    m_tCharging.CDRIndicator = "DATA";
                }
                else
                {
                    m_tCharging.Cost = 0;
                    m_util.write_log(Request.Url.OriginalString, 2);
                    m_util.WritelogHeader(Request.Headers);
                    m_tCharging.CDRIndicator = "WRONG";
                }
                if (Int32.Parse(tinh_id) < 1000)
                {
                    string temp = "";
                    temp = m_db.GetXsByTinh(tinh_id);
                    if (temp != "0")
                    {
                        str += "<page>" + m_util.xulyStringXS(temp);
                    }
                    else
                    {
                        str += "<page>" +
                        "Xin Loi! Noi Dung nay hien chua co,quy khach vui long quay lai sau!";
                        m_tCharging.Cost = 0;
                        m_tCharging.CDRIndicator = "WRONG";
                    }
                    m_db.InsertChargingTbl(m_tCharging);
                }
                else
                {
                    tinh_id = tinh_id.Remove(0, 1);
                    str += "<page> Nhap ngay bat ky" +
                        "<form action='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + tinh_id + "'>" +
                        //"<form action='Xoso_KetQua.aspx?Tinhid=004'>" +
                        "<entry kind='digits' var='day'>" +
                        "<prompt> Ngay can tim </prompt>" +
                        "</entry>" +
                        "</form>";
                }
            }
            else
            {
                if (Service_id == 21)
                {
                    //To day  
                    m_tSession.ServiceList = "21";
                    m_tSession.KeyPress = "1";
                    m_db.UpdateReQuestTbl(m_tSession);
                    DataSet ds = new DataSet();
                    ds = m_db.GetListTinhToDay(0);
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    str += "<page>To day <br/>";
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + dr["Ma_Xo_So"].ToString() + "'>" + dr["Tinh"].ToString().Trim() + "</a><br/>";
                    }

                }
                else if (Service_id == 22)
                {
                    //Mien Bac 
                    m_tSession.ServiceList = "22";
                    m_tSession.KeyPress = "2";
                    m_db.UpdateReQuestTbl(m_tSession);
                    str += "<page>MienBac <br/>";
                    str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=004'>Hom Nay</a><br/>";
                    //+ "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=1004'>Ngay Bat Ky</a><br/>";
                    //+ "<a href='Xoso_KetQua.aspx?Tinhid=1004'>Ngay Bat Ky</a><br/>";
                }
                else if (Service_id == 23)
                {
                    //Mien Nam
                    DataSet ds = new DataSet();
                    ds = m_db.GetListTinhToDay(3);
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    str += "<page>Mien Nam <br/>";
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + dr["Ma_Xo_So"].ToString() + "'>" + dr["Tinh"].ToString().Trim() + "</a><br/>";
                    }
                }
                else if (Service_id == 24)
                {
                    //Mien Trung
                    m_tSession.ServiceList = "24";
                    m_tSession.KeyPress = "4";
                    m_db.UpdateReQuestTbl(m_tSession);
                    DataSet ds = new DataSet();
                    ds = m_db.GetListTinhToDay(2);
                    DataTable dtb = new DataTable();
                    dtb = ds.Tables[0].Clone();
                    str += "<page>Mien Trung <br/>";
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                    }
                    foreach (DataRow dr in dtb.Rows)
                    {
                        str += "<a href='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + dr["Ma_Xo_So"].ToString() + "'>" + dr["Tinh"].ToString().Trim() + "</a><br/>";
                    }
                }
                else
                {
                    str += "<page>" +
                        "Xin Loi! Noi Dung nay hien chua co,quy khach vui long quay lai sau!";
                }
            }
            str += "</page>" +
                "</pages>";
            Response.ClearHeaders();
            Response.AddHeader("content-type", "text/xml");
            Response.Write(str);
            wf.WriteStreamFile(str);
            Response.End();
        }
        catch (Exception ex)
        {
            wf.WriteError(ex.ToString());
        }
    }
}